package com.zzb.framework.mail;

import com.sun.mail.util.MailSSLSocketFactory;
import com.zzb.framework.param.service.SysParamService;
import com.zzb.framework.utils.SpringContextHolder;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.security.GeneralSecurityException;
import java.util.Properties;

/**
 * email配置上下文
 * Created by zhangzhenbin on 17-7-17.
 */
public class EmailPropertiesContext {
    private static String account;//登录用户名
    private static String pass;        //登录密码
    private static String from;        //发件地址
    private static String host;        //服务器地址
    private static String port;        //端口
    private static String protocol; //协议
    private static String personalName;//发邮件用户名
    private static Properties prop;
    private static MyAuthenricator myAuthenricator;

    static {
        reload();
    }

    public static void reload() {
        SysParamService sysParamService = SpringContextHolder.getBean("sysParamService");
        account = sysParamService.getString("MAIL_USERNAME", "xxx@tuandai.com");
        pass = sysParamService.getString("MAIL_PASSWORD", "xxxx");
        from = sysParamService.getString("MAIL_USERNAME", "xxx@tuandai.com");
        host = sysParamService.getString("MAIL_HOST", "smtp.exmail.qq.com");
        port = sysParamService.getString("MAIL_PORT", "465");
        protocol = sysParamService.getString("MAIL_PROTOCOL", "smtp");
        personalName = sysParamService.getString("PERSONAL_NAME","XXX");

        prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", protocol);
        //服务器
        prop.setProperty("mail.smtp.host", host);
        //端口
        prop.setProperty("mail.smtp.port", port);
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        myAuthenricator = new MyAuthenricator(account, pass);
    }

    public static String getPersonalName() {
        return personalName;
    }

    public static String getFrom() {
        return from;
    }

    public static String getAccount() {
        return account;
    }

    public static String getPass() {
        return pass;
    }

    public static String getHost() {
        return host;
    }

    public static String getPort() {
        return port;
    }

    public static String getProtocol() {
        return protocol;
    }

    public static Properties getProp() {
        return prop;
    }

    /**
     * 获取邮件配置
     */
    public static Properties getProperties() {
        return prop;
    }

    /**
     * 获取用户名密码验证对象
     */
    public static MyAuthenricator getMyAuthenricator(){
        return myAuthenricator;
    }


    //用户名密码验证，需要实现抽象类Authenticator的抽象方法PasswordAuthentication
    static class MyAuthenricator extends Authenticator {
        String u = null;
        String p = null;

        MyAuthenricator(String u, String p) {
            this.u = u;
            this.p = p;
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(u, p);
        }
    }

}
